package com.yanqu.road.dataHelper.action.excel;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.ActivityMgr;
import com.yanqu.road.dataHelper.manager.BaseMgr;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Chess extends BaseRepair {

    public Chess(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }

    public void goAllActivity(){
        go();
    }

    private void go(){
        List<Map<String, String>> listMap = new ArrayList<>();
        listMap = getUserSkinAddErr();
        listMap = getLogSkinUse(listMap);
        DataMgr.addData(listMap);
    }

    //
    private List<Map<String, String>> getLog(int activityId) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_l_chessmove WHERE opTypo = 6 AND score >= 1500 AND reward not like '%110209%' AND activityId = ?";
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i=1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("activityId",checkNullToEmpty(rs.getString("activityId")));
                temp.put("score",checkNullToEmpty(rs.getString("score")));
                temp.put("结束6",checkNullToEmpty(rs.getString("opTypo")));
                temp.put("reward",checkNullToEmpty(rs.getString("reward")));
                temp.put("time",checkNullToEmpty(rs.getString("time")));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private List<Map<String, String>> getLogSkin(int activityId) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_l_goods_2 WHERE goodsId = 120736 AND `date` > '2022-04-01' AND isGet = 0";
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i=1;
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private List<Map<String, String>> getUserSkin2(int activityId) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_u_activity_chinese_chess WHERE isBuyGoldToken = 1 AND chessSkin like '%120736%' AND activityId = ?";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i=1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private List<Map<String, String>> getUserSkin(List<Map<String, String>> inList,int activityId) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_u_activity_chinese_chess WHERE chessSkin not like '%120736%' AND userId = ? AND activityId = ?";
        for(Map<String, String> data : inList){
            String userId = data.get("userId");
            try {
                call = getGameServer().getConnection().prepareCall(sql);
                int i=1;
                call.setString(i++,userId);
                call.setInt(i++,activityId);
                rs = call.executeQuery();
                if (rs.next()) {
                    Map<String,String> temp = new ConcurrentHashMap<>();
                    temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                    temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                    temp.put("activityId",checkNullToEmpty(rs.getString("activityId")));
                    dataList.add(temp);
                }
            } catch (SQLException ex) {
                getGameServer().getLogger().error("异常错误", ex);
            } finally {
                getGameServer().closeConnection(call, rs);
            }
        }
        return dataList;
    }

    private List<Map<String, String>> getUserSkinAddErr() {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_u_activity_chinese_chess WHERE chessSkin like '%,12%' AND activityId in (9089981,9089982,9089983,9089984,9089985,9089986,9089987,9089988,9089989,9089990,9089991,9089992,9089993,9089994,9089995,9089996,9089997,9089998,9089999,9090000,9090001,9090002,9090003,9090004,9090005,9090006,9090007,9090008,9090009,9090010,9090011,9090012,90900139089981,9089982,9089983,9089984,9089985,9089986,9089987,9089988,9089989,9089990,9089991,9089992,9089993,9089994,9089995,9089996,9089997,9089998,9089999,9090000,9090001,9090002,9090003,9090004,9090005,9090006,9090007,9090008,9090009,9090010,9090011,9090012,9090013)";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i=1;
            rs = call.executeQuery();
            if (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("activityId",checkNullToEmpty(rs.getString("activityId")));
                temp.put("chessSkin",checkNullToEmpty(rs.getString("chessSkin")));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private List<Map<String, String>> getLogSkinUse(List<Map<String, String>> inList) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_l_goods_2 WHERE userId = ? AND goodsId >= 120730 AND goodsId <= 120746 AND `time` >= ? AND `time` <= ? AND isGet = 0";
        for(Map<String, String> data : inList){
            int activityId = Integer.valueOf(data.get("activityId"));
            BaseMgr.getMgr(ActivityMgr.class).initActivity(activityId);
            ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
            String start = DateHelper.getCurrentDateTimeString(activityInfo.getBeginShowTime() * 1000);
            String end = DateHelper.getCurrentDateTimeString(activityInfo.getEndShowTime() * 1000);
            String userId = data.get("userId");
            String skinData = data.get("chessSkin");
            try {
                call = getGameServer().getLogConnection().prepareCall(sql);
                int i=1;
                call.setString(i++,userId);
                call.setString(i++,start);
                call.setString(i++,end);
                rs = call.executeQuery();
                while (rs.next()) {
                    Map<String,String> temp = new ConcurrentHashMap<>();
                    String goodsId = checkNullToEmpty(rs.getString("goodsId"));
                    if(!skinData.contains(goodsId)){
                        temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                        temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                        temp.put("activityId",checkNullToEmpty(data.get("activityId")));
                        temp.put("goodsId",checkNullToEmpty(rs.getString("goodsId")));
                        dataList.add(temp);
                    }
                }
            } catch (SQLException ex) {
                getGameServer().getLogger().error("异常错误", ex);
            } finally {
                getGameServer().closeConnection(call, rs);
            }
        }
        return dataList;
    }

}
